from elgamal_core import ElGamal


if __name__ == '__main__':
    KEY_BITS = 2048
    print(f"开始 ElGamal 演示 (密钥位数: {KEY_BITS})...\n")
    # 实例化并生成密钥
    eg = ElGamal(KEY_BITS)
    p, q, g, y = eg.public_key
    x = eg.private_key

    print("\n--- 准备消息 ---")
    original = "你好，ElGamal！这是一个秘密消息。"
    print(f"原始消息: {original}")
    m_int = ElGamal.string_to_int(original)
    print(f"消息整数: {m_int}")
    if m_int >= p:
        raise ValueError("消息太长，请缩短或增大密钥位数")

    print("\n--- 加密 ---")
    c1, c2 = ElGamal.encrypt(eg.public_key, m_int)
    print(f"密文 (c1, c2): ({c1}, {c2})")

    print("\n--- 解密 ---")
    m2 = ElGamal.decrypt(x, (p, q, g), (c1, c2))
    print(f"解密后整数: {m2}")
    decrypted = ElGamal.int_to_string(m2)
    print(f"解密后消息: {decrypted}")

    print("\n--- 验证 ---")
    print("成功！" if decrypted == original else "失败。")

